package com.xyy.saas.payment.merchant.core.service;

import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.dao.model.UserInfo;
import com.xyy.saas.payment.merchant.core.vo.UserInfoVo;
import com.xyy.saas.payment.service.PayAccountService;
import com.xyy.saas.payment.service.UserInfoService;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
@Slf4j
public class UserInfoService2 {
    @Resource
    private UserInfoService userInfoService;
    @Autowired
    private AccountService accountService;
    @Resource
    private PayAccountService payAccountService;

    /**
     * 查询
     */
    public UserInfoVo queryUserInfo(String accountId) throws PaymentException {
        log.info("UserInfoService2#queryUserInfo accountId={}", accountId);

        // 1.查询
        UserInfo userInfo = userInfoService.queryUserInfoByAccountId(accountId);

        if(userInfo == null) {
            throw new PaymentException(ResultCodeEnum.USER_NOT_FOUND);
        }

        // 2.解析
        try {
            return UserInfoVo.transfer(userInfo);
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new PaymentException(ResultCodeEnum.USER_INFO_DECRYPT_ERROR);
        }
    }

    /**
     * 更新
     */
    public boolean updateUserInfo(UserInfoVo userInfoVo) throws PaymentException {
        log.info("UserInfoService2#updateUserInfo userInfoVo={}", JSONUtils.toJSON(userInfoVo));
        return userInfoService.updateByPrimaryKeySelective(UserInfoVo.transfer(userInfoVo)) > NumberUtils.INTEGER_ZERO;
    }

    /**
     * 获取授权人手机号
     */
    public String getOperatorPhone(String businessIdType, String businessId) {
        String accountId = payAccountService.getAccountIdByBusiness(businessIdType, businessId);

        if(StringUtils.isBlank(accountId)) {
            return null;
        }

        return getOperatorPhone(accountId);
    }

    /**
     * 获取授权人手机号
     */
    public String getOperatorPhone(String accountId) {
        if(!accountService.checkBaseOpenAccountComplete(accountId)) {
            return null;
        }

        try {
            UserInfoVo userInfoVo = queryUserInfo(accountId);

            if(userInfoVo == null) {
                return null;
            }

            if(userInfoVo.getOperatorInfo() != null && StringUtils.isNotBlank(userInfoVo.getOperatorInfo().getPhone())) {
                return userInfoVo.getOperatorInfo().getPhone();
            }
            else {
                return userInfoVo.getTelNo();
            }
        }
        catch(Exception ex) {
            log.error("UserInfoService2#getOperatorPhone accountId=" + accountId + ", message=" + ex.getMessage(), ex);
            return null;
        }
    }
}
